/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import javax.swing.DefaultComboBoxModel;
import data.Departments;
import bo.DepartmentsBO;
import data.Deptinterdependency;
import TableView.DeptinterdependencyTable;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.tree.TreeNode;
import dialogview.DeptinterdependencyDialog;
import dao.DepartmentDAO;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.PopupMenu;
import javax.swing.ImageIcon;
import javax.swing.event.TreeModelListener;
import javax.swing.text.Position;
import javax.swing.tree.TreePath;

/**
 *
 * @author ChiHieu
 */
public class DepartmentsDependenciesManager extends javax.swing.JFrame {

	DepartmentsBO dBO;
	public static DepartmentsDependenciesManager dM;

	/**
	 * Creates new form DepartmentsDependenciesManager
	 */
	public DepartmentsDependenciesManager() {
		initComponents();
		dBO = new DepartmentsBO();
		cbbDepNo.setModel(dBO.getComboBoxModel());
		cbbUpperDep.setModel(dBO.getComboBoxModel());
		cbbLowerDep.setModel(dBO.getComboBoxModel());
		showTableDepinterDependency();
		dBO.getListMutable();
		dM = this;
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cbbup = new javax.swing.JPanel();
        lbDepdependencies = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        btnCreate = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        cbbDepNo = new javax.swing.JComboBox();
        jScrollPane2 = new javax.swing.JScrollPane();
        tree = new javax.swing.JTree();
        cbbUpperDep = new javax.swing.JComboBox();
        cbbLowerDep = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableInterdependency = new javax.swing.JTable();
        btnEdit = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnShow = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        cbbup.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));

        lbDepdependencies.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        lbDepdependencies.setText("DEPARMENT DEPENDENCIES");

        jLabel1.setText("DepNo");

        jLabel2.setText("UpperDep");

        jLabel3.setText("LowerDept");

        btnCreate.setText("Create");
        btnCreate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateActionPerformed(evt);
            }
        });

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        cbbDepNo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        tree.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("Root");
        javax.swing.tree.DefaultMutableTreeNode treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("hieu");
        treeNode1.add(treeNode2);
        tree.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        tree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                treeValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(tree);

        cbbUpperDep.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        cbbLowerDep.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jScrollPane1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        tableInterdependency.setModel(new DeptinterdependencyTable());
        jScrollPane1.setViewportView(tableInterdependency);

        btnEdit.setText("Edit");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        btnShow.setText("Show");
        btnShow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnShowActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout cbbupLayout = new javax.swing.GroupLayout(cbbup);
        cbbup.setLayout(cbbupLayout);
        cbbupLayout.setHorizontalGroup(
            cbbupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(cbbupLayout.createSequentialGroup()
                .addGroup(cbbupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(cbbupLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 605, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(cbbupLayout.createSequentialGroup()
                        .addGap(384, 384, 384)
                        .addComponent(lbDepdependencies, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(cbbupLayout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addGroup(cbbupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(cbbupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel2))
                            .addGroup(cbbupLayout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(1, 1, 1)))
                        .addGap(58, 58, 58)
                        .addGroup(cbbupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(cbbupLayout.createSequentialGroup()
                                .addGroup(cbbupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(cbbDepNo, 0, 241, Short.MAX_VALUE)
                                    .addComponent(cbbUpperDep, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(cbbLowerDep, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGap(49, 49, 49))
                            .addGroup(cbbupLayout.createSequentialGroup()
                                .addComponent(btnCreate)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnCancel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                        .addGroup(cbbupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnShow)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 499, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(204, 225, Short.MAX_VALUE))
        );
        cbbupLayout.setVerticalGroup(
            cbbupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(cbbupLayout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(lbDepdependencies, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(3, 3, 3)
                .addComponent(btnShow)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(cbbupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(cbbupLayout.createSequentialGroup()
                        .addGroup(cbbupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cbbDepNo, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(47, 47, 47)
                        .addGroup(cbbupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(cbbUpperDep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(45, 45, 45)
                        .addGroup(cbbupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(cbbLowerDep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(35, 35, 35)
                        .addGroup(cbbupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnCreate)
                            .addComponent(btnEdit)
                            .addComponent(btnDelete)
                            .addComponent(btnCancel)))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(389, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cbbup, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(cbbup, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCreateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreateActionPerformed
		// TODO add your handling code here:
		if (dBO.actionCreateDEPTINTERDEPENDENCY(cbbDepNo.getSelectedItem().toString(),//depno
				cbbUpperDep.getSelectedItem().toString(),//depupper
				cbbLowerDep.getSelectedItem().toString())) {//depdn
		}
		JOptionPane.showMessageDialog(this, dBO.getInfomation());
		showTableDepinterDependency();

    }//GEN-LAST:event_btnCreateActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
		// TODO add your handling code here:
		this.setVisible(false);
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
		// TODO add your handling code here:
		DeptinterdependencyDialog dpdialog = new DeptinterdependencyDialog(this, true, Integer.parseInt(tableInterdependency.getValueAt(tableInterdependency.getSelectedRow(), 0).toString()));
		dpdialog.setVisible(true);
    }//GEN-LAST:event_btnEditActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
		// TODO add your handling code here:
		DepartmentDAO dDAO = new DepartmentDAO();
		Deptinterdependency dptmp = new Deptinterdependency();
		try {
			dptmp.setId(Integer.parseInt(tableInterdependency.getValueAt(tableInterdependency.getSelectedRow(), 0).toString()));
			dptmp.setUpdeptno(Integer.parseInt(tableInterdependency.getValueAt(tableInterdependency.getSelectedRow(), 1).toString()));
		} catch (Exception ee) {
			JOptionPane.showMessageDialog(this, "Please chosse object!");
		}
		dptmp.setDepartments(dDAO.getDepartmentByName(tableInterdependency.getValueAt(tableInterdependency.getSelectedRow(), 2).toString()));
		dBO.actionDeleteDepDependency(dptmp);
		JOptionPane.showMessageDialog(this, dBO.getInfomation());
		showTableDepinterDependency();
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void treeValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_treeValueChanged
		// TODO add your handling code here:
		for (DefaultMutableTreeNode dmt : dBO.getChildList(evt.getPath().getLastPathComponent().toString())) {
			//tree.add((Component) dmt.clone(), tree.getSelectionModel().getSelectionCount());
			DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) evt.getPath().getLastPathComponent();//
			selectedNode.add(dmt);
		}
		tree.expandPath(evt.getPath());//  
    }//GEN-LAST:event_treeValueChanged

    private void btnShowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnShowActionPerformed
		// TODO add your handling code here:
		tree.setModel(showhierarchy());
    }//GEN-LAST:event_btnShowActionPerformed

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
		 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(DepartmentsDependenciesManager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(DepartmentsDependenciesManager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(DepartmentsDependenciesManager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(DepartmentsDependenciesManager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		}
		//</editor-fold>

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new DepartmentsDependenciesManager().setVisible(true);
			}
		});
	}
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnCreate;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnShow;
    private javax.swing.JComboBox cbbDepNo;
    private javax.swing.JComboBox cbbLowerDep;
    private javax.swing.JComboBox cbbUpperDep;
    private javax.swing.JPanel cbbup;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lbDepdependencies;
    private javax.swing.JTable tableInterdependency;
    private javax.swing.JTree tree;
    // End of variables declaration//GEN-END:variables

	public void showTableDepinterDependency() {
		tableInterdependency.setModel(dBO.actionShowTableDepinterDependency());
	}

	public TreeModel showhierarchy() {
		DefaultMutableTreeNode Organization = new DefaultMutableTreeNode("Organization");
		for (Deptinterdependency dp : new dao.DepartmentDAO().getALLDepinterdependency()) {
			if (dp.getUpdeptno() == 0) {
				Organization.add(new DefaultMutableTreeNode(dp.getDepartments().getDeptname()));
			}
		}
		DefaultTreeModel model = new DefaultTreeModel(Organization);
		return model;
	}
}
